package com.kfm.servlet;

import com.kfm.control.JdbcSelect;
import com.kfm.tool.IpAddressValidator;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addServer")
public class AddServer extends HttpServlet {
    @SneakyThrows
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //请求重定向到服务器页面
        request.setCharacterEncoding("UTF-8");
        String sql = "insert into serverta (url,username,password,administrator,email,phone)values(?,?,?,?,?,?)";
        String address = request.getParameter("address");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String administrator = request.getParameter("administrator");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        if (address != null && username != null && password != null && administrator != null && email != null && phone != null) {
            if (IpAddressValidator.isIpAddress(address)) {
                JdbcSelect.insertiofo(sql, address, username, password, administrator, email, phone);
                JdbcSelect.insertInfoLogs("添加服务器"+" "+address);
                JdbcSelect.close();
            }
        }
        response.sendRedirect(request.getContextPath() + "/servlet.html");
    }
}
